package com.ruoyi.member.service.impl;

import com.ruoyi.member.domain.History;
import com.ruoyi.member.mapper.HistoryMapper;
import com.ruoyi.member.service.IHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;

/**
 * 观看历史Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-22
 */
@Service
public class HistoryServiceImpl implements IHistoryService {

    @Autowired
    private HistoryMapper historyMapper;

    /**
     * 查询观看历史
     *
     * @param id 观看历史主键
     * @return 观看历史
     */
    @Override
    public History selectHistoryById(Long id) {
        return historyMapper.selectHistoryById(id);
    }

    /**
     * 查询观看历史列表
     *
     * @param history 观看历史
     * @return 观看历史集合
     */
    @Override
    public List<History> selectHistoryList(History history) {
        return historyMapper.selectHistoryList(history);
    }

    /**
     * 查询用户观看历史
     *
     * @param userId 用户ID
     * @return 观看历史集合
     */
    @Override
    public List<History> selectHistoryByUserId(Long userId) {
        return historyMapper.selectHistoryByUserId(userId);
    }

    /**
     * 获取用户最近观看的影视
     *
     * @param userId 用户ID
     * @param limit 数量限制
     * @return 观看历史集合
     */
    @Override
    public List<History> selectRecentHistory(Long userId, int limit) {
        return historyMapper.selectRecentHistory(userId, limit);
    }

    /**
     * 获取用户对某影视的观看进度
     *
     * @param userId 用户ID
     * @param movieId 影视ID
     * @return 观看历史
     */
    @Override
    public History selectHistoryByUserAndMovie(Long userId, Long movieId) {
        return historyMapper.selectHistoryByUserAndMovie(userId, movieId);
    }

    /**
     * 新增观看历史
     *
     * @param history 观看历史
     * @return 结果
     */
    @Override
    public int insertHistory(History history) {
        history.setCreateTime(new Date());
        history.setLastWatchTime(new Date());
        return historyMapper.insertHistory(history);
    }

    /**
     * 修改观看历史
     *
     * @param history 观看历史
     * @return 结果
     */
    @Override
    public int updateHistory(History history) {
        history.setUpdateTime(new Date());
        history.setLastWatchTime(new Date());
        return historyMapper.updateHistory(history);
    }

    /**
     * 更新观看进度
     *
     * @param userId 用户ID
     * @param movieId 影视ID
     * @param watchTime 观看时间（秒）
     * @param totalTime 总时长（秒）
     * @return 结果
     */
    @Override
    public int updateWatchProgress(Long userId, Long movieId, Long watchTime, Long totalTime) {
        History history = historyMapper.selectHistoryByUserAndMovie(userId, movieId);
        if (history == null) {
            history = new History();
            history.setUserId(userId);
            history.setMovieId(movieId);
            history.setWatchTime(watchTime);
            history.setTotalTime(totalTime);
            history.setProgress(calculateProgress(watchTime, totalTime));
            return insertHistory(history);
        } else {
            history.setWatchTime(watchTime);
            history.setTotalTime(totalTime);
            history.setProgress(calculateProgress(watchTime, totalTime));
            return updateHistory(history);
        }
    }

    /**
     * 更新观看进度
     *
     * @param history 观看历史
     * @return 结果
     */
    @Override
    public int updateProgress(History history) {
        history.setProgress(calculateProgress(history.getWatchTime(), history.getTotalTime()));
        history.setUpdateTime(new Date());
        history.setLastWatchTime(new Date());
        return historyMapper.updateHistory(history);
    }

    /**
     * 批量删除观看历史
     *
     * @param ids 需要删除的观看历史主键集合
     * @return 结果
     */
    @Override
    public int deleteHistoryByIds(Long[] ids) {
        return historyMapper.deleteHistoryByIds(ids);
    }

    /**
     * 删除观看历史信息
     *
     * @param id 观看历史主键
     * @return 结果
     */
    @Override
    public int deleteHistoryById(Long id) {
        return historyMapper.deleteHistoryById(id);
    }

    /**
     * 清空用户观看历史
     *
     * @param userId 用户ID
     * @return 结果
     */
    @Override
    public int clearUserHistory(Long userId) {
        return historyMapper.deleteByUserId(userId);
    }

    /**
     * 清空会员观看历史
     *
     * @param memberId 会员ID
     * @return 结果
     */
    @Override
    public int clearHistoryByMemberId(Long memberId) {
        return historyMapper.deleteByMemberId(memberId);
    }

    /**
     * 获取会员继续观看列表
     *
     * @param memberId 会员ID
     * @return 观看历史列表
     */
    @Override
    public List<History> getContinueWatchingList(Long memberId) {
        return historyMapper.getContinueWatchingList(memberId);
    }

    /**
     * 计算观看进度百分比
     */
    private Integer calculateProgress(Long watchTime, Long totalTime) {
        if (watchTime == null || totalTime == null || totalTime == 0) {
            return 0;
        }
        return (int) (watchTime * 100 / totalTime);
    }
}